﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UITriggerRoot : MonoBehaviour
{
    public List<UITrigger> Triggers = new List<UITrigger>();
    public Dictionary<System.Type, List<UITrigger>> Events = new Dictionary<System.Type, List<UITrigger>>();

    static UITriggerRoot inst;
    SGLisp.Context UITriggerContext; 
    public static UITriggerRoot Inst() 
    {
        if (inst == null) 
        {
          inst = new GameObject("UITriggerRoot").AddComponent<UITriggerRoot>();
        }
        return inst;
    }
    void Awake() 
    {
        SGLisp.RT.InitRuntime();
        UITriggerContext = new SGLisp.Context(SGLisp.RT.MainContext);
    }

    public void AddTrigger(UITrigger trigger) 
    {
        this.Triggers.Add(trigger);
        for (int i = 0; i < trigger.TriggerEvents.Count; i++)
        {
           UITriggerEvent curEvent = trigger.TriggerEvents[i];
           if (!this.Events.ContainsKey(curEvent.GetType())) 
           {
               this.Events[curEvent.GetType()] = new List<UITrigger>();
           }
           this.Events[curEvent.GetType()].Add(trigger);
        }
    }

    public void CallEvent(System.Type eventType,Hashtable argDic) 
    {
        if (this.Events.ContainsKey(eventType)) 
        {
           List<UITrigger> triggers  =this.Events[eventType];
           for (int i = 0; i < triggers.Count;i++) 
           {
               triggers[i].OnTrigger(argDic);
           }
        }
    }
}
